package com.um.psystem.service.mtService.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.um.psystem.entity.mtEntity.AssetsDetail;
import com.um.psystem.entity.mtEntity.AssetsType;
import com.um.psystem.mapper.platform.PublicMapper;
import com.um.psystem.mapper.platform.mtManageMapper.AssetsDetailMapper;
import com.um.psystem.model.vo.JsonResult;
import com.um.psystem.service.mtService.IAssetsDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zzj
 * @Description: 物资明细实现类
 * @Date: 2020/5/29
 */
@Service
public class AssetsDetailService implements IAssetsDetailService {

    @Autowired
    AssetsDetailMapper assetsDetailMapper;
    @Autowired
    PublicMapper publicMapper;

    @Override
    public JsonResult<Integer> save(AssetsDetail assetsDtail) {
        return JsonResult.success(assetsDetailMapper.insert(assetsDtail));
    }

    @Override
    public JsonResult<Integer> update(AssetsDetail assetsDtail) {
        return JsonResult.success(assetsDetailMapper.updateById(assetsDtail));
    }

    @Override
    public JsonResult<Integer> del(Integer id) {
        Integer delNum = assetsDetailMapper.delete(new EntityWrapper<AssetsDetail>()
                .eq("type_dtl_id",id));
        return JsonResult.success(delNum);
    }

    @Override
    public JsonResult<AssetsDetail> get(Integer id) {
        AssetsDetail assetsDtail =  assetsDetailMapper.selectById(id);
        return JsonResult.success(assetsDtail);
    }

    @Override
    public List<AssetsDetail> getAssetsDetails(Map<String, Object> columnMap) {
        EntityWrapper<AssetsDetail> ew = new EntityWrapper<AssetsDetail>();
        if(StrUtil.isNotBlank(columnMap.get("type_main_id")!=null?columnMap.get("type_main_id").toString():null)){
            ew.eq("type_main_id",columnMap.get("type_main_id").toString());
        }
        if(StrUtil.isNotBlank(columnMap.get("type_dtl_name")!=null?columnMap.get("type_dtl_name").toString():null)){
            ew.like("type_dtl_name",columnMap.get("type_dtl_name").toString());
        }
        List<AssetsDetail> assetsDetailList =  assetsDetailMapper.selectList(ew);
        return assetsDetailList;
    }

    @Override
    public String getTypeDtlNo(Map<String, Object> columnMap) {
        Map paramsMap = new HashMap();
        String sql = "select CONCAT(b.type_main_no,lpad(a.type_dtl_num+1,4,0),'X') as typeDtlNo_new " +
                "from ws_eng_assets_type_dtl a " +
                "join ws_eng_assets_type_main b " +
                "on a.type_main_id=b.type_main_id " +
                "where b.type_main_id=#{type_main_id} " +
                "order by a.type_dtl_num desc limit 1";
        paramsMap.put("sqlStr",sql);
        paramsMap.put("type_main_id",columnMap.get("type_main_id").toString());
        List<LinkedHashMap<String, Object>> typeList = publicMapper.getPublicItems(paramsMap);
        if (typeList.size()>0){
            return typeList.get(0).get("typeDtlNo_new").toString();
        }else {
            sql = "select type_main_no from ws_eng_assets_type_main where type_main_id=#{type_main_id}";
            paramsMap.put("sqlStr",sql);
            List<LinkedHashMap<String, Object>> typeMain_list = publicMapper.getPublicItems(paramsMap);
            return typeMain_list.get(0).get("type_main_no").toString()+"0001X";
        }
    }

    @Override
    public String getTypeDtlNoExist(String typeDtlNo) {
        Map paramsMap = new HashMap();
        String sql = "select type_dtl_no " +
                "from ws_eng_assets_type_dtl " +
                "where type_dtl_no=#{typeDtlNo}";
        paramsMap.put("sqlStr",sql);
        paramsMap.put("typeDtlNo",typeDtlNo);
        List<LinkedHashMap<String, Object>> typeList = publicMapper.getPublicItems(paramsMap);
        if (typeList.size()>0){
            return typeList.get(0).get("type_dtl_no").toString();
        }else {
            return "";
        }
    }
}
